using System;
using System.Collections.Generic;
using Gtk;
using AtDevice;

namespace GtkControls
{
    
    [System.ComponentModel.Category("GtkControls")]
    [System.ComponentModel.ToolboxItem(true)]
    public partial class TextMessageWidget : Gtk.Bin
    {
        private Gtk.NodeStore _messages = new Gtk.NodeStore(typeof(TextMessageNode));
        private MessageStatus _currentStatus = MessageStatus.All;
        private ISMSInterface _smsInterface;
        private TextBuffer _messageViewBuffer; 
        
        public TextMessageWidget()
        {
            this.Build();
            SetColumns();
            SetEnables();
            SetupBuffer();
            
            DeviceHandler.DeviceOpened += delegate {
                _smsInterface = DeviceHandler.Get().GetSMSInteface();
                RefreshMessages();
            };
            
            messageList.NodeSelection.Changed += delegate {
                SetEnables();
                DisplaySelectedNodes();
            };
        }
        
        private void SetColumns()
        {
            messageList.AppendColumn("Name/number", new Gtk.CellRendererText(), "text", 0);
            messageList.AppendColumn("Content", new Gtk.CellRendererText(), "text", 1);
            messageList.AppendColumn("Time", new Gtk.CellRendererText(), "text", 2);
            messageList.NodeStore = _messages;
        }
        
        private void SetupBuffer()
        {
            _messageViewBuffer = messageView.Buffer;
            TextTag boldTag = new TextTag("bold");
            boldTag.Weight = Pango.Weight.Bold;
            _messageViewBuffer.TagTable.Add(boldTag);
        }
        
        private void RefreshMessages()
        {
            _messages.Clear();
            
            foreach (var message in _smsInterface.ListMessages(_currentStatus)) {
                _messages.AddNode(new TextMessageNode(message));
            }
        }
        
        private void DisplaySelectedNodes()
        {
            _messageViewBuffer.Clear();
            
            if(messageList.NodeSelection.SelectedNodes.Length > 0)
            {
                TextMessageNode sms = (TextMessageNode)messageList.NodeSelection.SelectedNode;
                ShowInPane(sms.SMS);
            }
        }
        
        private void SetEnables()
        {
            if(messageList.NodeSelection.SelectedNodes.Length > 0)
            {
                replyButton.Sensitive = true;
                forwardButton.Sensitive = true;
                deleteMessagesButton.Sensitive = true;
            }
            else
            {                
                replyButton.Sensitive = false;
                forwardButton.Sensitive = false;
                deleteMessagesButton.Sensitive = false;
            }
        }
        
        private void AppendLineToPane(string label, string content)
        {
            TextIter end = _messageViewBuffer.EndIter;
            _messageViewBuffer.InsertWithTagsByName(ref end, label + ": ", "bold");
            _messageViewBuffer.Insert(ref end, content + "\n");
        }
        
        private void ShowInPane(SMS sms)
        {
            AppendLineToPane("Number", sms.Number);
            AppendLineToPane("Time", sms.TimeStamp);
            AppendLineToPane("Status", sms.Status.ToString());
            AppendLineToPane("Content", sms.Content);
        }

        protected virtual void OnDeleteMessagesButtonClicked (object sender, System.EventArgs e)
        {
            foreach(TextMessageNode node in messageList.NodeSelection.SelectedNodes)
            {
                _smsInterface.DeleteSMS(node.SMS.Identifier);
                _messages.RemoveNode(node);
            }
        }

        protected virtual void OnFolderComboBoxChanged (object sender, System.EventArgs e)
        {
            switch(folderComboBox.ActiveText)
            {
            case "All":
                _currentStatus = MessageStatus.All;
                break;
            case "Inbox":
                _currentStatus = MessageStatus.Received;
                break;
            case "Outbox":
                _currentStatus = MessageStatus.StoredSent;
                break;
            case "Drafts":
                _currentStatus = MessageStatus.StoredUnsent;
                break;
            case "Deleted":
                break;
            case "Reports":
                break;
            default:
                throw new NotSupportedException("Should not get here");
            }
            
            RefreshMessages();
        }

        protected virtual void OnNewMessageButtonClicked (object sender, System.EventArgs e)
        {
            new TextMessageNewWindow("", "").Show();
        }

        protected virtual void OnReplyButtonClicked (object sender, System.EventArgs e)
        {            
            TextMessageNode sms = (TextMessageNode)messageList.NodeSelection.SelectedNode;
            new TextMessageNewWindow(sms.Number, "").Show();
        }

        protected virtual void OnForwardButtonClicked (object sender, System.EventArgs e)
        {
            TextMessageNode sms = (TextMessageNode)messageList.NodeSelection.SelectedNode;
            new TextMessageNewWindow("", sms.Content).Show();
        }
    }
}
